package Boke2;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadPool {
    //用于储存任务的阻塞队列
    private BlockingQueue<Runnable> queue=new LinkedBlockingQueue<>();
    //将提交的任务添加到队列中
    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }

    //创建指定线程个数，并执行队列中的任务
    public ThreadPool(int ThreadCount) {
        for (int i = 0; i < ThreadCount; i++) {
            Thread t1 = new Thread(() -> {
                while(!Thread.currentThread().isInterrupted()){
                    try {
                        Runnable task=queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }

                }

            });
            t1.start();
        }
    }



    public static void main(String[] args) throws InterruptedException {
        ThreadPool pool=new ThreadPool(10);
        for (int i = 0; i < 50; i++) {
            pool.submit(new Runnable(){
                @Override
                public void run() {
                    System.out.println("hello");
                }
            });
        }
        Thread.sleep(5000);
        for (int i = 0; i < 50; i++) {
            pool.submit(new Runnable(){
                @Override
                public void run() {
                    System.out.println("-------------");
                }
            });
        }
    }
}
